package thread;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description：内存可见性问题
 * User：chenshu
 * Date：2023-11-07
 * Time：23:00
 */
public class Demo2 {
    static class Counter {
        volatile static int flag;
    }
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            while (Counter.flag == 0) {
                
            }
            System.out.println("t1结束");
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个整数:");
            Counter.flag = scanner.nextInt();
        });
        t2.start();
    }
}
